package com.jsgt.greekfire.common.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.util.TypedValue
import androidx.appcompat.widget.AppCompatTextView
import com.fhx.core.utils.ScreenUtil.dip2px
import com.jsgt.greekfire.R

class TabTextView(context: Context, attrs: AttributeSet?) : AppCompatTextView(context, attrs) {
    var checkColor = 0
    var normalColor = 0
    var mCheck = false//是否选中
    var underline = true
    var underlineColor = 0
    var lineRect: RectF? = null
    var paint: Paint? = null
    var lineMaxWidth = -1
    var normal_size = 0
    var checked_size = 0

    init {
        init(context, attrs)
    }

    private fun init(
        context: Context,
        attrs: AttributeSet?
    ) {
        lineMaxWidth = dip2px(context, 20f).toInt()
        if (attrs != null && attrs.attributeCount > 0) {
            val typedArray =
                context.obtainStyledAttributes(attrs, R.styleable.TabTextView)
            if (typedArray != null) {
                checkColor = typedArray.getColor(R.styleable.TabTextView_check_color, 0)
                normalColor = typedArray.getColor(R.styleable.TabTextView_normal_color, 0)
                val checked =
                    typedArray.getBoolean(R.styleable.TabTextView_checked, false)
                underline = typedArray.getBoolean(R.styleable.TabTextView_underline, true)
                normal_size =
                    typedArray.getDimensionPixelSize(R.styleable.TabTextView_normal_size, 0)
                checked_size =
                    typedArray.getDimensionPixelSize(R.styleable.TabTextView_checked_size, 0)
                typedArray.recycle()
                setChecked(checked)
            }
        }
    }

    fun setChecked(checked: Boolean) {
        mCheck = checked
        isEnabled = !checked
        if (checked) {
            setTextColor(normalColor)
            if (checked_size > 0) {
                setTextSize(TypedValue.COMPLEX_UNIT_PX, checked_size.toFloat())
            }
            false
        } else {
            if (normal_size > 0) {
                setTextSize(TypedValue.COMPLEX_UNIT_PX, normal_size.toFloat())
            }
            setTextColor(normalColor)
            true
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (paint == null) paint = Paint()
        if (mCheck) {
            paint!!.color = checkColor
        }
        if (underline) {
            if (lineRect == null) {
                lineRect = RectF()
                lineRect!!.top = height - dip2px(context, 1f)
                val textWidth = getPaint().measureText(text.toString())
                val lineWidth: Float
                lineWidth = if (lineMaxWidth > 0) {
                    Math.min(textWidth, lineMaxWidth.toFloat())
                } else {
                    textWidth
                }
                val centerX = width / 2
                lineRect!!.left = centerX - lineWidth / 2
                lineRect!!.right = lineRect!!.left + lineWidth
                lineRect!!.bottom = height.toFloat()
            }
            if (mCheck) {
                canvas.drawRect(lineRect!!, paint!!)
            }
        }
    }
}